<?php
/**
 * Created by PhpStorm.
 * User: dust2
 * Date: 15-1-3
 * Time: 下午4:05
 */

/**
 * 订单过滤，主要针对订单状态的更改进行过滤，过滤准则为当前登入用户角色是否为管理员
 * Class OrderFilter
 */
class OrderFilter
{
    public function filter()
    {
        $Action = explode('@', Route::currentRouteAction())[1];
        $func = $Action.'Filter';
        return $this->$func();
    }

    /**
     * 订单更新过滤
     */
    public function updateFilter()
    {
        if (! $this->arrayIncludeValue('administrator', Role::getUserRoleNames())) {
            return $this->failResponse('无操作权限.');
        }
    }

    /**
     * 订单删除过滤
     */
    public  function destroyFilter()
    {
        if (! $this->arrayIncludeValue('administrator', Role::getUserRoleNames())) {
            return $this->failResponse('无操作权限.');
        }
    }

    /**
     * 访问未定义方法拦截器
     * @param $method
     * @param $args
     * @return \Illuminate\Http\JsonResponse
     */
    function __call($method, $args)
    {
        return $this->failResponse('错误访问');
    }



    /**
     * 返回过滤结果提示
     * @return \Illuminate\Http\JsonResponse
     */
    protected function failResponse($message)
    {
        return Response::json([
            'status' => 'failure',
            'message' => $message
        ]);
    }

    protected function arrayIncludeValue($value, $arg)
    {
        foreach ($arg as $per) {
            if (in_array($value, $per)) {
                return true;
            }
        }

        return false;
    }
}